package com.example.startapp.fragment;

import android.content.Context;
import android.graphics.Outline;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.startapp.R;
import com.example.startapp.util.DBOpenHelper;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

/**
 * 这是首页
 */

public class HomeFragment extends Fragment {
    private Banner banner;
    private View view;
    private Inflater inflater;
    private static final String TAG = "HomeFragment";
    private RecyclerView recyclerView;
    private DBOpenHelper mDBOpenHelper;
    String appNames = "这是图片";
    String[] names = {
            "立体浮雕新年款猫和老鼠小米红米note8pro手机壳男软壳女k20潮牌",
            "皮卡丘奶油胶手机壳适用于Huawei/华为 Mate30pro 安卓各种型号",
            "鼠年新年款苹果11手机壳iPhone11Pro MAX滴胶女潮牌猫和老鼠",
            "红米note8pro手机壳红米note8保护套全包防摔红米note7硬壳超薄磨砂红米"
    };
    String[] price = {
            "23",
            "69",
            "28",
            "22"
    };
    int[] pics = {
            R.mipmap.one,
            R.mipmap.two,
            R.mipmap.three,
            R.mipmap.four
    };

    public HomeFragment() {
        // Required empty public constructor
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.home, null);
        mDBOpenHelper=new DBOpenHelper(getActivity());
        banner = view.findViewById(R.id.banner01);
        recyclerView = view.findViewById(R.id.recycler);

        //两列排列
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));

        MyAdapter myAdapter = new MyAdapter();
        recyclerView.setAdapter(myAdapter);
        initBanner();



        return view;


    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView t_headline, t_label, t_price, t_personNum;
            ImageView imageView;

            public MyViewHolder(@NonNull final View itemView) {
                super(itemView);
                t_headline = itemView.findViewById(R.id.headline);
                t_label = itemView.findViewById(R.id.label);
                t_price = itemView.findViewById(R.id.price);
                imageView = itemView.findViewById(R.id.goodsimage);
                t_personNum=itemView.findViewById(R.id.personNum);
//
            }


        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(getActivity()).
                    inflate(R.layout.home_item, parent, false));
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int position) {
            myViewHolder.t_headline.setText(names[position]);
            myViewHolder.t_label.setText("每满300减40");
            myViewHolder.t_price.setText("￥" + price[position] + ".00");
            myViewHolder.imageView.setImageResource(pics[position]);
            myViewHolder.t_personNum.setText(price[position]+2+"付款");
        }

        @Override
        public int getItemCount() {
            return names.length;
        }
    }


    /**
     * banner
     */
    private List<Integer> images = new ArrayList<>();

    public void initBanner() {
        banner.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 30);
            }
        });

        banner.setClipToOutline(true);

        images.add(R.mipmap.one);
        images.add(R.mipmap.two);
        images.add(R.mipmap.three);
        images.add(R.mipmap.four);

        banner.setImages(images);
        banner.setImageLoader(new My());
        banner.start();

    }

    class My extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).into(imageView);
        }
    }


}
